import pandas as pd


# 交易账户
class hpQuant(object):
    def __init__(self):  # 类初始化
        self.order_df = None  # 下单流水
        self.trade_df = None  # 交易流水
        self.security_df = None  # 持仓清单
        self.money2 = None  # 总资金
        self.money = None  # 资金
        self.priceBuy = 0.00  # 最后一次买入价格
        self.priceSell = 999999.00  # 最后一次卖出价格
        self.amount = 0.00  # 证券数量
        self.code = ""  # 证券代码
        self.trading_Commission = None  # 交易佣金
        self.stamp_duty = None  # 印花税
        self.position = False  # 持仓状态
        self.stop_loss_on = None  # 允许止损
        self.priceStopLoss = None  # 止损价
        self.stop_loss_range = None  # 止损幅度
        self.stop_loss_num = 0  # 当前止损次数
        self.stop_loss_max = None  # 最大允许止损次数.到止损次数,就停止交易
        self.trade = True  # 允许交易
        self.Init()

    def Init(self):  # 初始化交易数据
        self.order_df = pd.DataFrame(columns=['date', 'time', 'mode', 'code', 'amount', 'price'])#订单
        self.order_df = self.order_df.reset_index(level=None, drop=True, col_level=0, col_fill='')#重置索引
        self.trade_df = pd.DataFrame(columns=['date', 'time', 'mode', 'code', 'amount', 'price', 'money'])#交易
        self.trade_df = self.trade_df.reset_index(level=None, drop=True, col_level=0, col_fill='')
        self.security_df = pd.DataFrame(columns=['code', 'amount', 'price', 'money'])#股票
        self.security_df = self.security_df.reset_index(level=None, drop=True, col_level=0, col_fill='')



    def Order(self, date, time, mode, code, amount, price):  # 交易函数
        '''写入订单'''
        ln = len(self.order_df)
        df_new = pd.DataFrame({'date': date, 'time': time, 'mode': mode, 'code': code, 'amount': amount,'price': price}, index=[ln])
        self.order_df = self.order_df.append(df_new, ignore_index=True)

        '''交易'''
        ln = len(self.trade_df)
        if mode == 1:  #模式1为 买入
            se = amount * price * (1 + self.trading_Commission)#买入花费（交易佣金目前为0）
            self.money = self.money - se#现金减少
            df_new = pd.DataFrame({'date': date, 'time': time, 'mode': mode, 'code': code, 'amount': amount, 'price': price, 'money': self.money}, index=[ln])#更新现金数
            self.trade_df = self.trade_df.append(df_new, ignore_index=True)
            '''分购买股票之前没有/有两种情况更新数据'''
            if len(self.security_df[self.security_df.code == code]) == 0:
                df_new = pd.DataFrame({'code': code, 'amount': amount, 'price': se / amount, 'money': se}, index=[ln])
                self.security_df = self.security_df.append(df_new, ignore_index=True)
            else:
                self.security_df.index = self.security_df['code']
                self.security_df.loc[code, 'amount'] = self.security_df.loc[code, 'amount'] + amount
                self.security_df = self.security_df.reset_index(level=None, drop=True, col_level=0, col_fill='')

        ln = len(self.security_df[self.security_df.code == code])
        if mode == 2 and ln > 0:  # 模式2为卖出
            self.security_df.index = self.security_df['code']
            am = self.security_df.loc[code, 'amount']
            if am < amount:
                amount = am
            se = amount * price * (1 - self.trading_Commission - self.stamp_duty)
            self.money = self.money + se
            df_new = pd.DataFrame({'date': date, 'time': time, 'mode': mode, 'code': code, 'amount': amount, 'price': price, 'money': self.money}, index=[ln])
            self.trade_df = self.trade_df.append(df_new, ignore_index=True)
            am2 = self.security_df.loc[code, 'amount'] - amount
            self.security_df.loc[code, 'amount'] = am2
            if am2 == 0:
                self.security_df = self.security_df.drop(code, axis=0)
                # 在行的维度上删除行
            self.security_df = self.security_df.reset_index(level=None, drop=True, col_level=0, col_fill='')
